package org.example.erat.service.parser;

import org.apache.commons.csv.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class CsvParser implements FileParser {

    @Override
    public List<Student> parse(InputStream inputStream) {
        List<Student> students = new ArrayList<>();
        try (Reader in = new InputStreamReader(inputStream);
             CSVParser parser = new CSVParser(in, CSVFormat.DEFAULT.withFirstRecordAsHeader())) {

            for (CSVRecord record : parser) {
                String id = record.get(0).trim();
                String name = record.get(1).trim();
                String major = record.get(2).trim();
                students.add(new Student(id, name, major));
            }

        } catch (IOException e) {
            throw new RuntimeException("CSV解析失败：" + e.getMessage(), e);
        }
        return students;
    }
}
